#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__author__ = "Administrator"
__name__="mtsleep4"
__date__="2016/9/18"
__time__="16:51"
"""
# 二：创建一个Thread的实例，传给它一个可调用的类对象
import threading
from time import sleep,ctime

loops = [4,2]

class ThreadFunc(object):
    def __init__(self,func,args,name=''):
        self.name = name
        self.func = func
        self.args = args

    def __call__(self):
        apply(self.func,self.args)  # apply(loop,(i,loops[i]))

def loop(nloop,nsec):
    print 'start loop',nloop,'at:',ctime()
    sleep(nsec)
    print 'loop',nloop,'done at:',ctime()

def main():
    print 'starting at:',ctime()
    threads = []
    nloops = range(len(loops))

    for i in nloops:
        print "loop:%s,i:%s,loopsi:%s" % (loop,i,loops[i])
        t = threading.Thread(target=ThreadFunc(loop,(i,loops[i]),loop.__name__))    # 简化为：target=ThreadFunc()  --这样写法就调用了call方法，里面的参数传递给apply方法。
        threads.append(t)

    for i in nloops:
        threads[i].start()
    for i in nloops:
        threads[i].join()
    print 'all DONE at:',ctime()

if __name__ == '__main__':
    main()
